package com.sise.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sise.entity.ProductInfo;
import com.sise.entity.Rating;
import com.sise.mapper.ProductInfoMapper;
import com.sise.mapper.RatingMapper;
import com.sise.service.IRatingService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zj
 */
@Service
public class RatingServiceImpl extends ServiceImpl<RatingMapper, Rating> implements IRatingService {

    @Resource
    private RatingMapper ratingMapper;

    @Resource
    private ProductInfoMapper productInfoMapper;

    @Override
    public List<ProductInfo> recommend(String openId) {
        List<Rating> ratingList = ratingMapper.getRatingByUserOrderByDescNotIn0(new Rating().setUser(openId));
        List<ProductInfo> productInfoList = new ArrayList<>();
        if (ratingList.size() < 5) {
            for (Rating rating : ratingList) {
                productInfoList.add(productInfoMapper.selectById(rating.getItem()));
            }
            List<ProductInfo> topNProductInfo = productInfoMapper.randomlyGenerateNProductInfo(5 - ratingList.size());
            for (int i = 0; i < 5 - ratingList.size(); i++) {
                productInfoList.add(topNProductInfo.get(i));
            }
        } else {
            for (int i = 0; i < 5; i++) {
                productInfoList.add(productInfoMapper.selectById(ratingList.get(i).getItem()));
            }
        }
        return productInfoList;
    }
}
